package com.limao.AsyncListener;

import java.io.IOException;

import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletResponse;

import com.limao.Model.PublicData;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;
import com.limao.Utils.OutPrintClass;

@WebListener
public class AppAsyncListener implements AsyncListener{

	private RecvData DataObj;
	private long startTime;
	
	public AppAsyncListener(){}
	public AppAsyncListener(RecvData DataObj)
	{
		this.DataObj=DataObj;
		this.startTime=CacheClass.getTime();
	}
	
	 
	      @Override
	      public void onComplete(AsyncEvent event) throws IOException {
	         //System.out.println("异步调用完成……"+"用时："+(CacheClass.getTime()-this.startTime)+"毫秒");
	      
	        // event.getSuppliedResponse().getWriter().println("异步调用完成……");
	      }
	 
	      @Override
	      public void onError(AsyncEvent event) throws IOException {
	         System.out.println("异步调用出错……");
	         //event.getSuppliedResponse().getWriter().println("异步调用出错……");
	      }
	 
	      @Override
	      public void onStartAsync(AsyncEvent event) throws IOException {
	         System.out.println("异步调用开始……");
	         
	         //event.getSuppliedResponse().getWriter().println("异步调用开始……");
	      }
	 
	      @Override
	      public void onTimeout(AsyncEvent event) throws IOException {
	          if(DataObj!=null)
	          {
	        	HttpServletResponse response= (HttpServletResponse) event.getSuppliedResponse();
				PublicData jsonObj = new PublicData();
				jsonObj.m = DataObj.m;
				jsonObj.state = "0";
				jsonObj.info = "调用超时……";
				OutPrintClass.writer(response, jsonObj);
	          }
	          System.out.println("异步调用超时……"+"用时："+(CacheClass.getTime()-this.startTime)+"毫秒");
	      }



}
